package com.scse.bms.web.admin;

import com.alibaba.fastjson.JSONObject;
import com.scse.bms.domain.ThemeVO;
import com.scse.bms.service.ThemeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ChartsController
 * @Author: 吴赠禹
 * @Date: 2021/3/2 14:37
 * @Version: 1.0
 * @Description: TODO
 */

@Slf4j
@RestController
public class ChartsController {

    @Autowired
    private ThemeService themeService;

    private List<String> xData;
    private List<Integer> series1;
    private List<Integer> series2;
    private List<Integer> series3;


    /**
     * 请求主题一般数据
     * @return
     */
    @GetMapping("/theme/charts/general")
    public String getThemeSeriesChart() {
        log.info("[getThemeSeriesChart] Ajax请求主题数据");
        List<ThemeVO> list = themeService.getThemeList();
        initList();
        for (int i = 0; i < list.size(); i++) {
            xData.add(list.get(i).getName());
            series1.add(list.get(i).getBookNum());
            series2.add(list.get(i).getVisitedCount());
            series3.add(list.get(i).getDownloadCount());
        }
        JSONObject bean = new JSONObject();
        bean.put("themes", xData);
        bean.put("bookNums", series1);
        bean.put("visitedCounts", series2);
        bean.put("downloadCounts", series3);
        destroyList();
        log.info("[getThemeSeriesChart] 返回数据：" + bean.toJSONString());
        destroyList();
        return bean.toString();
    }


    /**
     * 请求访问历史数据
     * @return
     */
    @GetMapping("/theme/charts/visitedAvg")
    public String getVisitedAvg() {
        log.info("[getVisitedAvg] 请求获取主题访问平均数据");
        List<ThemeVO> list = themeService.getThemeVisitedAvgData();
        JSONObject bean = fillData(list);
        log.info("[getVisitedAvg] 返回数据：" + bean.toJSONString());
        return bean.toString();
    }

    /**
     * 请求访问下载数据
     */
    @GetMapping("/theme/charts/downloadAvg")
    public String getDownloadAvg() {
        log.info("[getDownloadAvg] 请求获取主题历史下载数据");
        List<ThemeVO> list = themeService.getThemeDownloadData();
        JSONObject bean = fillData(list);
        log.info("[getDownloadAvg] 返回数据：" + bean.toJSONString());
        return bean.toString();
    }


    @GetMapping("/theme/charts/recommended")
    public String getRecommended() {
        log.info("[getRecommended] 请求获取已推荐的图书数量");
        List<ThemeVO> list = themeService.getThemeRecommend();
        JSONObject bean = new JSONObject();
        bean.put("list", list);
        log.info("[getRecommended] 返回数据：" + bean.toJSONString());
        return bean.toString();
    }


    private JSONObject fillData(List<ThemeVO> list) {
        initList();
        for (ThemeVO themeVO : list) {
            xData.add(themeVO.getName());
            series1.add(themeVO.getDayAvg());
            series2.add(themeVO.getMonAvg());
            series3.add(themeVO.getYearAvg());
        }
        JSONObject bean = new JSONObject();
        bean.put("themes", xData);
        bean.put("dayAvgList", series1);
        bean.put("monAvgList", series2);
        bean.put("yearAvgList", series3);
        destroyList();
        return bean;
    }


    /**
     * 初始化列表
     */
    private void initList() {
        xData = new ArrayList<>(10);
        series1 = new ArrayList<>(10);
        series2 = new ArrayList<>(10);
        series3 = new ArrayList<>(10);
    }

    /**
     * 释放列表（垃圾回收）
     */
    private void destroyList() {
        xData = null;
        series1 = null;
        series2 = null;
        series3 = null;
    }

}
